import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.zip.DataFormatException;

/**
 * 会议预订系统
 * @Author czx
 * @Date 2025/3/3
 * @Time 15:10
 */
public class Reservationsystem {
    private List<MeetingRoom> meetingRooms;//会议室信息
    private List<Reservation> reservations;//预定信息

    public Reservationsystem(List<MeetingRoom> meetingRooms) {
        this.meetingRooms = meetingRooms;
        this.reservations = new ArrayList<>();
    }

    /**
     * 查看所有会议室
     */
    public void showAllMeetingRooms() {
        System.out.println("---所有会议室列表---");
        System.out.println("名称              容纳人数            多媒体设备");
        for (MeetingRoom meetingRoom : meetingRooms) {
            System.out.println(meetingRoom);
        }
        System.out.println("-------------");
    }

    /**
     * 预定会议室
     */
    public void reserveMeetingRoom(){
        for(int i=0;i<meetingRooms.size();i++){
            MeetingRoom room=meetingRooms.get(i);
            System.out.println((i+1)+". "+room.getName()+"(容纳人数："+room.getCapacity()+",多媒体设备："
                    +(room.hasMultimedia()?"有":"无")+")");
            //选择会议室
            System.out.println("请选择要预定的会议室：");
            Scanner sc=new Scanner(System.in);
            int roomNo=sc.nextInt();
            MeetingRoom selectionRoom=meetingRooms.get(roomNo-1);
            //选择预定日期
            System.out.println("已选择"+selectionRoom.getName()+",");
            System.out.println("请选择要预定的日期(yyyy-mm-dd)：");
            //读入日期字符串
            String dateStr=sc.next();
            LocalDate date=LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));

            //读入时间字符串
            System.out.print("请选择预定时间段（例如：10：00-11：00）:");
            String timeStr=sc.next();
            String[] time=timeStr.split("-");
            LocalTime startTime=LocalTime.parse(time[0],DateTimeFormatter.ofPattern("HH:mm"));
            LocalTime endTime=LocalTime.parse(time[1],DateTimeFormatter.ofPattern("HH:mm"));

            //判断该会议室该时间段是否被预定
            boolean avaliable=isAvailable(selectionRoom,date,startTime,endTime);
            if(avaliable){//未被预定
                //创建预定信息
                Reservation reservation=new Reservation(selectionRoom,date,startTime,endTime);
                reservations.add(reservation);
                System.out.println("预定成功！");
            }
        }
    }

    /**
     * 判断该会议室该时间段是否被预定
     * @param selectionRoom 会议室
     * @param date 预定日期
     * @param startTime 预定开始日期
     * @param endTime 预定结束日期
     * @return 是否被预定
     */
    private boolean isAvailable(MeetingRoom selectionRoom, LocalDate date, LocalTime startTime, LocalTime endTime) {
        //默认返回可用
        boolean avaliable=true;
        //获取预定信息
        //集合 -> 流形式访问
        List<Reservation> reservationList=reservations.stream()
                .filter(r->r.getMeetingRoom().equals(selectionRoom))
                .toList();
        if(reservationList.isEmpty()){
            return true;
        }

        //会议室找到，预定时间是否已被预订
        for(Reservation r:reservationList){
            if(date.equals(r.getDate())){
                //时间段是否被预定
                if(startTime.isBefore(r.getEndTime())&&endTime.isAfter(r.getStartTime())){
                    System.out.println("该时间段会议室已被预订，请选择其他时间段或会议室");
                    avaliable=false;
                    break;
                }
            }
        }
        return avaliable;
    }

    /**
     * 显示会议室预订情况
     */
    public void showResInfo(){
        System.out.println("---所有预定信息---");
        for(MeetingRoom meetingRoom:meetingRooms){
            System.out.println(meetingRoom.getName());
            List<Reservation>roomReservations=reservations.stream()
                    .filter(r->r.getMeetingRoom().equals(meetingRoom))
                    .collect(Collectors.toList());
            for(Reservation reservation:roomReservations){
                System.out.println(reservation);
            }
            if(roomReservations==null){
                System.out.println("该会议室无预定信息");
            }
        }
        System.out.println("------------");
    }

    /**
     * 查看某段时间内可用的会议室
     */
    public void selectRoomByTime(){
        Scanner sc=new Scanner(System.in);
        System.out.println("请按日期格式输入(yyyy-MM-dd):");
        String dateStr=sc.next();
        LocalDate date=LocalDate.parse(dateStr,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println("请选择预定时间段（例如：10：00-11：00）:");
        String timeStr=sc.next();
        String[] time=timeStr.split("-");
        LocalTime startTime=LocalTime.parse(time[0],DateTimeFormatter.ofPattern("HH:mm"));
        LocalTime endTime=LocalTime.parse(time[1],DateTimeFormatter.ofPattern("HH:mm"));
        //指定时间与已预定时间段冲突的会议室
        List<Reservation> conflictList=reservations.stream()
                .filter(r->r.getDate().equals(date))
                .filter(r->r.getStartTime().isBefore(endTime)&&r.getEndTime().isAfter(startTime))
                .collect(Collectors.toList());
        List<MeetingRoom> conflictRoom=conflictList.stream()
                .map(Reservation::getMeetingRoom)
                .collect(Collectors.toList());
        //输出所有与指定时间段不冲突的会议室
        System.out.println("名称              容纳人数            多媒体设备");
        for (MeetingRoom meetingRoom:meetingRooms){
            if(!conflictRoom.contains(meetingRoom)){
                System.out.println(meetingRoom);
            }
        }
    }
}
